using System;
using System.Collections.Generic;
using System.Text;
using AntEater.ComponentModel.BuildElements;
using System.Xml.XPath;
using System.Xml;
using AntEater.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace AntEater.Runtime.Services.Transformation
{

    /// <summary>
    /// Provides a transfomer for the ChooseBuildElement.
    /// </summary>
    [BuildElementTransformer(typeof(ChooseBuildElement), XPathNodeType.Element)]
    internal class ChooseTransformer:BuildElementTransformerBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ChooseTransformer"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public ChooseTransformer(object context)
            : base(context) {
        }

        /// <summary>
        /// Transforms an <c>BuildElement</c> into XML.
        /// </summary>
        /// <param name="buildElement">The build element.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="errors">The errors.</param>
        public override void GetXml(IBuildElement buildElement, XmlWriter writer, List<TransformationError> errors) {
            ChooseBuildElement element = buildElement as ChooseBuildElement;

            if (element != null) {
            
                BuildElementAttribute buildElementAttribute
                    = ComponentModel.Utilities.GetTypeAttributeFromObject<BuildElementAttribute>(buildElement);

                if (buildElementAttribute != null) {
                    writer.WriteStartElement(buildElementAttribute.Name);
                    
                    this.OnGetChildElementsXml(element, writer);
                    
                    writer.WriteEndElement();
                }
            }
        }

        /// <summary>
        /// Gets an <c>BuildElement</c> from XML.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="buildFileDefinition"></param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public override IBuildElement GetBuildElement(XPathNavigator node, BuildFileDefinition buildFileDefinition, List<TransformationError> errors) {
            ChooseBuildElement element = buildFileDefinition.CreateNewBuildElement(typeof(ChooseBuildElement)) as ChooseBuildElement;
                        
            return element;
        }
    }
}
